package com.x.compute.handle;

import cn.hutool.core.collection.CollectionUtil;
import com.x.compute.common.util.ComputeUtils;
import com.x.compute.core.context.VariableContext;
import com.x.compute.core.entity.ComputeColumn;
import com.x.compute.core.handle.column.ColumnVarHandle;
import com.x.compute.core.service.ComputeService;
import com.x.compute.entity.Product;
import com.x.compute.entity.ProductColumn;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Product列数据集
 * @author xuxin
 */
@Service
public class ProductColumnHandle extends ColumnVarHandle {

    @Resource
    private ComputeService computeService;

    @Override
    public String getTableId() {
        return VariableContext.getTableId(Product.class);
    }

    @Override
    public List<? extends ComputeColumn> dataSource(Long formId, Map<String, Object> referenceValueMap) {
        List<Product> productList = selectList(formId);
        if (CollectionUtil.isEmpty(productList)){
            return null;
        }
        List<ProductColumn> dataSet = new ArrayList<>();
        List<String> businessIds = productList.stream().map(data -> data.getId().toString()).collect(Collectors.toList());
        Map<String, String> varValueMap = computeService.getVarValue(formId, businessIds);
        productList.forEach(product -> {
            ProductColumn column = new ProductColumn();
            //方法一，手动处理
            column.setId(product.getId());
            column.setQuantity(varValueMap.get(ComputeUtils.getVarFullName("quantity", product.getId().toString())));
            column.setUnitPrice(varValueMap.get(ComputeUtils.getVarFullName("unitPrice", product.getId().toString())));
            //方法二，反射注入
//            valueReflection(product.getId(), column, varValueMap);
            dataSet.add(column);
        });
        return dataSet;
    }

    /**
     * 模拟查询
     * @param formId
     * @return
     */
    private List<Product> selectList(Long formId) {
        return new ArrayList<>();
    }
}
